<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$form = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>">
    <input type="button" onclick="javascript:preview()" value="Preview" />
    <?php
    if ($form != null) {
    ?>
    <input type="hidden" name="id"
        value="<?php Printer::display($form->getId()); ?>" />
    <?php
    }
    ?>
    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            value="<?php if ($form != null) Printer::display($form->getName()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Texte :</span>
        <textarea class="firstInput" name="text"><?php
            if ($form != null) Printer::display($form->getText());
        ?></textarea>
    </div>
    <div class="label">
        <span class="titre">Thèmes :</span>
        <div class="formPartsOrderer" id="formParts">
            <ul class="formPartsList">
                <?php 
                if ($form != null) {
                    $logger = new Logger("profile");
                    $parts = $form->getParts();
                    $parts = $form->sortParts();

                    if (!is_null($form) && !is_null($parts)) {
                        for ($i=0; $i<count($parts); $i++) {
                            $part = $parts[$i] ;
                            $theme = $part->getTheme();
                            $points = $part->getPoints();
                            if (isNullOrEmptyString($points)) { $points = '0'; }
                            $price = $part->getPrice();
                            
                            if (!is_null($theme)) {
                                ?>
                <li>
                    <span class="item">
                        <?php Printer::display($theme->getName()); ?>
                        <input
                            class="id"
                            type="hidden"
                            name="part_id[]"
                            value="<?php Printer::display($theme->getId()); ?>" />
                         - 
                         <label>
                             Points de remplissage : 
                             <input
                                 type="text"
                                 name="points[]" size="2"
                                 value="<?php Printer::display($points); ?>"
                                 />
                        </label>
                        - 
                         <label>
                             Prix du thème : 
                             <input
                                 type="text"
                                 name="price[]" size="2"
                                 value="<?php Printer::display($price); ?>"
                                 />
                        </label>
                        <input
                            class="order"
                            type="hidden"
                            name="part_order[]"
                            value="<?php Printer::display($i+1); ?>" />
                    </span>
                </li>
                <?php 
                            }
                        }
                    }
                }
                ?>
            </ul>
        </div>
    </div>
    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    function preview(){
        var url = '<?php echo HTML::linkHref('action=PreviewTheme&profile_id='.$form->getId()); ?>';
        window.open(url, "preview","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=600");
    }
    $(document).ready(function() {
        $("#formParts .formPartsList").formPartsOrderer(
            {
                "serviceUrl":"?service=ListForms&format=json",
                "container":"#formParts",
                "fields":[
                    {
                        "type":"text",
                        "name":"points",
                        "label":"Points de remplissage",
                        "size":"2",
                        "defaultValue":"0"
                    },
                    {
                        "type":"text",
                        "name":"price",
                        "label":"Prix du thème",
                        "size":"2"
                    }
                ]
            }
        );
    });
</script>
